package com.kx.mybatis.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kx.mybatis.mapper.UserMapper;
import com.kx.mybatis.pojo.MemberUser;
import com.kx.mybatis.service.UserService;
import com.kx.mybatis.utils.MybatisUtils;

import java.util.List;

/**
 * @Description
 * @ClassName UserServiceImpl
 * @Author Sun
 * @Date 2021/5/15 17:55
 * @Version 1.0
 */
public class UserServiceImpl implements UserService {
    private UserMapper userMapper = MybatisUtils.getMapper(UserMapper.class);

    @Override
    public MemberUser getUserById(String id) {
        if (id == null || "".equals(id.trim())) {
            return null;
        }
        try {
            MemberUser user = userMapper.getUserById(Long.parseLong(id));
            return user;
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public PageInfo<MemberUser> findAllUsers(String pageIndex, String pageSize) {
        //开启分页
        PageHelper.startPage(Integer.parseInt(pageIndex), Integer.parseInt(pageSize));
        List<MemberUser> users= userMapper.selectUsers();
        PageInfo<MemberUser> pageInfo = new PageInfo<>(users);
        return pageInfo;
    }

    @Override
    public void addUser(MemberUser user) {
        if (user == null) {
            throw new RuntimeException();
        }
        UserMapper userMapper = MybatisUtils.getMapper(UserMapper.class);
        userMapper.insertUser(user);
        MybatisUtils.commit();

    }
}
